package com.jprocms.module.cms.dal.mysql.site;

import com.jpro.framework.mybatis.core.mapper.BaseMapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jprocms.module.cms.controller.admin.site.vo.SiteQueryReqVO;
import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import org.apache.ibatis.annotations.CacheNamespaceRef;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 站点 Mapper
 *
 * @author jprocms
 */
@CacheNamespaceRef(SiteMapper.class)
@Mapper
public interface SiteMapper extends BaseMapperX<SiteDO> {

    List<SiteDO> selectSiteList(@Param("params") SiteQueryReqVO reqVO);

    List<SiteDO> selectByIds(@Param("ids") Collection<Long> ids);

    default SiteDO selectByParentIdAndName(Long parentId, String name) {
        return selectOne(new LambdaQueryWrapper<SiteDO>().eq(SiteDO::getParentId, parentId)
                .eq(SiteDO::getName, name));
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(SiteDO::getParentId, parentId);
    }

    SiteDO selectById(Long id);

    @Select("SELECT COUNT(*) FROM cms_site WHERE update_time > #{maxUpdateTime}")
    Long selectCountByUpdateTimeGt(LocalDateTime maxTime);
}
